package cjq.demo.spring.aop;

import org.junit.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * author: cjianquan
 * date: 2016/9/9
 */
public class AopTest {
    @Test
    public void test_staticProxy(){
        Greeting greetingProxy = new GreetingProxy(new GreetingImpl());
        greetingProxy.sayHello("Jack");
    }

    @Test
    public void test_jdkDynamriProxy(){
        Greeting greeting = new JDKDynamicProxy(new GreetingImpl()).getProxy();
        greeting.sayHello("Jack");
    }

    @Test
    public void test_jdkDynamriProxy4NoImpl(){
        GreetingNoImpl greeting = new JDKDynamicProxy(new GreetingNoImpl()).getProxy();
        greeting.sayHello("Jack");
    }

    @Test
    public void test_cglibDynamicProxy(){
        Greeting greeting = CGLibDynamicProxy.getInstance().getProxy(GreetingImpl.class);
        greeting.sayHello("Jack");
    }

    @Test
    public void test_springAop(){
        ProxyFactory proxyFactory = new ProxyFactory();     // 创建代理工厂
        proxyFactory.setTarget(new GreetingImpl());         // 射入目标类对象
        proxyFactory.addAdvice(new GreetingBeforeAdvice()); // 添加前置增强
        proxyFactory.addAdvice(new GreetingAfterAdvice());  // 添加后置增强

        Greeting greeting = (Greeting) proxyFactory.getProxy(); // 从代理工厂中获取代理
        greeting.sayHello("Jack");
    }

    @Test
    public void test_springAop1(){
        ProxyFactory proxyFactory = new ProxyFactory();     // 创建代理工厂
        proxyFactory.setTarget(new GreetingImpl());         // 射入目标类对象
        proxyFactory.addAdvice(new GreetingBeforeAndAfterAdvice()); // 添加增强

        Greeting greeting = (Greeting) proxyFactory.getProxy(); // 从代理工厂中获取代理
        greeting.sayHello("Jack");
    }

    @Test
    public void test_greetingAroundAdvice(){
        ProxyFactory proxyFactory = new ProxyFactory();     // 创建代理工厂
        proxyFactory.setTarget(new GreetingImpl());         // 射入目标类对象
        proxyFactory.addAdvice(new GreetingAroundAdvice()); // 添加增强

        Greeting greeting = (Greeting) proxyFactory.getProxy(); // 从代理工厂中获取代理
        greeting.sayHello("Jack");
    }

    @Test
    public void test_aop_xml(){
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-aop.xml"); // 获取 Spring Context
//        ApplicationContext context = new FileSystemXmlApplicationContext("E:\\ideaWorkspace\\test\\src\\main\\resources\\spring-aop.xml");
        Greeting greeting = (Greeting) context.getBean("greetingProxy");                        // 从 Context 中根据 id 获取 Bean 对象（其实就是一个代理）
        greeting.sayHello("Jack");

        Apology apology = (Apology) greeting; // 将目标类强制向上转型为 Apology 接口（这是引入增强给我们带来的特性，也就是“接口动态实现”功能）
        apology.saySorry("Jack");
    }

    public void test_advised(){
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.addAdvisor(new DefaultPointcutAdvisor());
    }

}
